home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 June / Macworld (1999-06).dmg / Shareware World / Info / 1984 OLM Issue 12 / 1984 OLM Issue 12.rsrc / TEXT_148.txt < prev    next >
Text File  |  1999-04-20  |  13KB  |  253 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14. Backing Up With AppleScript
  15. Extra large blockbuster feature for all you script lovers
  16. Steve Harris
  17.  
  18. This article is one of many published in 1984-Online over the last year on the subject of AppleScript. I am not going to go over the basics of AppleScript, as this was done in issues 2 to 6 of 1984-Online, which are readily downloadable from 1984-Online‚Äôs web site. Don‚Äôt be put off, though, I always make the assumption that nobody bothers reading them anyway and write like you‚Äôre all Class A novices. And rightly so.
  19.  
  20. Many articles about backing up your data begin with some patronising lecture about how you must be several brain cells short of a glass of water if you don‚Äôt back up your data at least twenty times a day, perhaps accompanied by a predictable tale about some poor bloke who lost all his data and went bankrupt, then his wife left him, his children disowned him and his dog moved in with his mistress and so on, ad noisome.
  21.  
  22. Fortunately, we at 1984-Online Magazine are not here to patronise you or tell you stories, we‚Äôre just here to help your Mac using life be as pleasurable jargon‚Äìfree and acronym-free as is humanly possible. As a Mac user, you are spared endless hours tinkering with the anarchic and obscure bits of computers some so-called modern operating systems call ‚Äòpower features‚Äô. The Mac OS and AppleScript prove that power and ease-of-use needn‚Äôt be mutually incompatible. However, whether your computer is geek or chic backing up is a very good idea.
  23.  
  24. I know, I know, I know, it‚Äôs so dull remembering to copy files around every so often, rooting around in the depths of your Mac‚Äôs filing system trying to get it all together, but AppleScript is here to help. All you need is some form of removable storage ‚Äî a Zip drive would be ideal and a real bargain at under ¬£100 ‚Äî and any version of the Mac OS / System with AppleScript installed.
  25.  
  26. So without wanting to sound patronising backing up your data, in English, means making a copy of it for safe keeping. Before I begin, though, I have a small tale to tell of some poor bloke who didn‚Äôt make backups, lost all his data and went bankrupt. Then his wife‚Ķ (CUT!)
  27.  
  28. Laziness For An Enlightened Generation
  29. We are going to create an AppleScript script which backs up your data, and we‚Äôre going to do it in several stages. The first stage will be to create a usable backup script, and the rest of the stages will look at polishing the script to make your life even easier. At every stage your script should be usable (if only just) so it‚Äôs up to you how much polishing you do.
  30.  
  31. If this all sounds a bit daunting then don‚Äôt worry, it won‚Äôt be, I promise. Provided you know how to copy files and type a few words, it‚Äôs a cinch.
  32.  
  33. Getting Started
  34. First work out what files / folders you want to back up. This is up to you. For my own purposes I‚Äôm going to back up my Documents folder (53MB) and my email folders (about 8MB). As this script is simple, you‚Äôll need to find out how much you‚Äôre backing up and ensure that the space they‚Äôll occupy will not exceed the maximum space available on your backup disk. In my case I‚Äôll be backing-up about 61MB of data, which will fit nicely onto a 100MB Zip disk.
  35.  
  36. A few tips: if you‚Äôre thinking of backing up your email files and space is tight, remember to use the compacting features included in both Netscape Communicator and Outlook Express before you backup as this can often (especially in Netscape‚Äôs case) save megabytes of wasted rubbish. I can‚Äôt speak for other ‚Äòproper‚Äô email programs as I never use them. 
  37.  
  38. Netscape‚Äôs Mail (and indeed all your Netscape settings) are stored within a folder with your user name in the Netscape Users folder in your Preferences folder in your System Folder (deep breath), whereas Outlook Express‚Äôs mail is stored within a folder (probably called ‚ÄúMain User‚Äù) within a folder called OE User(s) in your Outlook Express folder.
  39.  
  40. Once you‚Äôve established what you want to backup and whether you‚Äôve got enough space to do it in one script, we‚Äôre ready to go.
  41.  
  42. Pop your disk in (I‚Äôm using a Zip) and give it an appropriate name. I‚Äôve called mine ‚ÄúBackup Disk‚Äù.
  43.  
  44. Record Breaker
  45. To create the main bulk of the script we‚Äôre going to record the manual process of backing up. To do this:
  46.  
  47. Open the Script Editor (you should find it in the AppleScript folder of your Apple Extras folder), and click Record.
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. Now drag the relevant folders from your hard disk to your backup disk, if you‚Äôve got Mac OS 8.5 or above, you don‚Äôt have to wait until one set of files has copied before copying the rest.
  63.  
  64. Go back to the Script Editor and click Stop (you don‚Äôt have to wait until the copying has finished). And voila! You have a usable backup script! 
  65.  
  66. Well, sort of. The script makes a few assumptions. Firstly, it assumes that the backup disk is empty, as an AppleScript, recorded as ours was, won‚Äôt replace files which already exist. Secondly, it assumes you want to see all the windows you opened and closed as you recorded the script opening and closing while you backup. It might look pretty, but it could become annoying. Thirdly, it assumes the files will copy before AppleScript times-out the operation and sulks. This last assumption needs some explanation.
  67.  
  68. The way AppleScript works is to give instructions to programs and then wait until they‚Äôve done it. Unfortunately, AppleScript is impatient and wants everything done now, and will wait no longer than one minute before barking some curt remark and giving you the cold shoulder treatment. Sounds like someone I know rather too well, but whose name I daren‚Äôt mention for fear of‚Ķ OUCH! What was that for?
  69.  
  70. However, unlike ‚Äòshe who must be obeyed‚Äô, you can override this behaviour in AppleScript. Ah, but if only real life were so simple.
  71.  
  72. So what we need to do is cut down the script, whilst keeping its functionality intact and add a small command to stop it being impatient along with a minor tweak to replace existing files whilst copying.
  73.  
  74. Take The Rubbish Out, Dear
  75. If you value the script you recorded, it would be sensible to save your script now, and make a copy of it to work on the next bit.
  76.  
  77. Amongst all the opening and closing of windows are the few important commands you really need to perform your backup. Let‚Äôs cut out all the rubbish and see what we‚Äôre left with.
  78.  
  79. tell application "Finder"
  80.     activate
  81.     
  82.     select folder "Documents" of startup disk
  83.     copy selection to disk "Backup Disk"
  84.     
  85.     select folder "Steve Harris" of folder "Netscape Users" of folder "Preferences" of folder "System Folder" of startup disk
  86.     copy selection to disk "Backup Disk"
  87.     
  88.     select folder "OE User(s)" of folder "Outlook Express 4.5 Folder" of folder "Microsoft Internet 4.5" of folder "Internet Applications" of folder "Internet" of startup disk
  89.     copy selection to disk "Backup Disk"
  90.     
  91. end tell
  92.  
  93. All I‚Äôve done is taken out all the lines apart from the ‚Äòselect‚Äô line before the ‚Äòcopy‚Äô lines, and the ‚Äòcopy‚Äô lines themselves. The lines I‚Äôve removed are highlighted in red, below:
  94.  
  95. tell application "Finder"
  96.     activate
  97.     select startup disk
  98.     open selection
  99.     select folder "Documents" of startup disk
  100.     copy selection to disk "Backup Disk"
  101.     select folder "System Folder" of startup disk
  102.     open selection
  103.     close container window of startup disk
  104.     select folder "Preferences" of folder "System Folder" of startup disk
  105.     open selection
  106.     close container window of folder "System Folder" of startup disk
  107.     select folder "Netscape Users" of folder "Preferences" of folder "System Folder" of startup disk
  108.     open selection
  109.     close container window of folder "Preferences" of folder "System Folder" of startup disk
  110.     select folder "Steve Harris" of folder "Netscape Users" of folder "Preferences" of folder "System Folder" of startup disk
  111.     copy selection to disk "Backup Disk"
  112.     close container window of disk "Backup Disk"
  113.     close container window of folder "Netscape Users" of folder "Preferences" of folder "System Folder" of startup disk
  114.     select startup disk
  115.     open selection
  116.     select folder "Internet" of startup disk
  117.     open selection
  118.     close container window of startup disk
  119.     select folder "Internet Applications" of folder "Internet" of startup disk
  120.     open selection
  121.     close container window of folder "Internet" of startup disk
  122.     select folder "Microsoft Internet 4.5" of folder "Internet Applications" of folder "Internet" of startup disk
  123.     open selection
  124.     close container window of folder "Internet Applications" of folder "Internet" of startup disk
  125.     select folder "Outlook Express 4.5 Folder" of folder "Microsoft Internet 4.5" of folder "Internet Applications" of folder "Internet" of startup disk
  126.     open selection
  127.     close container window of folder "Microsoft Internet 4.5" of folder "Internet Applications" of folder "Internet" of startup disk
  128.     select folder "OE User(s)" of folder "Outlook Express 4.5 Folder" of folder "Microsoft Internet 4.5" of folder "Internet Applications" of folder "Internet" of startup disk
  129.     copy selection to disk "Backup Disk"
  130.     close container window of folder "Outlook Express 4.5 Folder" of folder "Microsoft Internet 4.5" of folder "Internet Applications" of folder "Internet" of startup disk
  131. end tell
  132.  
  133. We can go even further and replace the word ‚Äòselection‚Äô on the ‚Äòcopy‚Äô lines with all the words on the ‚Äòselect‚Äô lines apart from the word ‚Äòselect‚Äô itself. Does that make any sense? Maybe seeing the script after that‚Äôs been done will clarify it:
  134.  
  135. tell application "Finder"
  136.     activate
  137.     
  138.     copy folder "Documents" of startup disk to disk "Backup Disk"
  139.     
  140.     copy folder "Steve Harris" of folder "Netscape Users" of ¬¨
  141.         folder "Preferences" of folder "System Folder" of ¬¨
  142.         startup disk to disk "Backup Disk"
  143.     
  144.     copy folder "OE User(s)" of folder ¬¨
  145.         "Outlook Express 4.5 Folder" of folder "Microsoft Internet 4.5" of folder ¬¨
  146.         "Internet Applications" of folder "Internet" of startup disk ¬¨
  147.         to disk "Backup Disk"
  148.     
  149. end tell
  150.  
  151. Note: You can split long lines to make the easier to read by pressing Command-Return. This inserts a ¬¨ character to show the line‚Äôs been split.
  152.  
  153. Adding Patience
  154. Next let‚Äôs add the two lines which will stop AppleScript being such an impatient old nag. By default AppleScript times-out after 60 seconds. Let‚Äôs set the timeout time to ten minutes (if you still have time-out problems, you can up the timeout period as appropriate):
  155. After the line which reads ‚Äòactivate‚Äô add this:
  156.     with timeout of 600 seconds
  157. And before the line which reads ‚Äòend tell‚Äô, add this:
  158.     end timeout
  159. Now click the ‚ÄòCheck Syntax‚Äô button to make it look all pretty again.
  160.  
  161. Replacing The Files As We Copy
  162. In order to replace the files that are there, we need to fiddle with the copy lines in the script: 
  163. Firstly change ‚Äòcopy‚Äô to ‚Äòduplicate‚Äô.
  164. Now add ‚Äòwith replacing‚Äô onto the end of the ‚Äòduplicate‚Äô lines, so they look something like this:
  165.         duplicate folder "Steve Harris" of folder "Netscape Users" of ¬¨
  166.             folder "Preferences" of folder "System Folder" of ¬¨
  167.             startup disk to disk "Backup Disk" with replacing
  168.  
  169. The Final Draft
  170. We‚Äôve made a lot of changes, here is my script in its entirety:
  171.  
  172. tell application "Finder"
  173.     activate
  174.     
  175.     with timeout of 600 seconds
  176.         duplicate folder "Documents" of startup disk to disk "Backup Disk" with replacing
  177.         
  178.         duplicate folder "Steve Harris" of folder "Netscape Users" of ¬¨
  179.             folder "Preferences" of folder "System Folder" of ¬¨
  180.             startup disk to disk "Backup Disk" with replacing
  181.         
  182.         duplicate folder "OE User(s)" of folder ¬¨
  183.             "Outlook Express 4.5 Folder" of folder "Microsoft Internet 4.5" of folder ¬¨
  184.             "Internet Applications" of folder "Internet" of startup disk ¬¨
  185.             to disk "Backup Disk" with replacing
  186.     end timeout
  187.     
  188. end tell
  189.  
  190.  
  191. Saving The Script
  192. Before you save your script, it would be nice to put a description in the description box at the top of the script window as this can appear in a box when you run the script. Here‚Äôs my description:
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203. In order that your script can be run like any other program, you‚Äôll need to save it as an application. To do this:
  204.  
  205. Choose Save As‚Ķ from the File menu.
  206. Choose where you want to save your script in the usual way.
  207. Choose Application from the Kind popup menu.
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227. Click Save.
  228. When you run the script, you should see a message looking something like this:
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244. Gluttons For Punishment
  245. Next month, for the brave and / or foolish amongst you, we‚Äôll look at further polishing of the script, which will include how to handle things when something goes wrong, which, in programming terms can be as important as backing up. Remember the sad tale of that poor bloke who didn‚Äôt handle his programming errors properly, lost all his data, and went bankrupt. Then his wife‚Ķ (CUT!)
  246. Ô£øSteve Harris
  247. <steve_harris@1984-online.com>
  248.  
  249.  ¬†
  250.   
  251.  
  252.  
  253.